$(function () {
    let isUsernameReady=false,
        isPasswordReady=false,
        isSmsCode=false,
        isMobileReady=false;

    let $image=$('.form-contain .form-item .captcha-graph-img img');
        $image.click(function () {
            $image.attr('src','/image/image_code/?rand='+ Math.random())
        });

    let user=$('#username');
        user.blur(fnCheckUsename);
        function fnCheckUsename() {
            isUsernameReady=false;
            let username=user.val();
            if(username===''){
                message.showError("用户名不能为空");
                return
            }
            if(!(/\w{5,20}$/).test(username)){
                message.showError("用户名长度错误");
                return
            }
            $.ajax({
                url:'/image/username/'+username+"/",
                type:"GET",
                dataType:"json",
                success:function (data) {
                    if(data.data.count ===0){
                        message.showInfo(username+"用户名可用");
                        isUsernameReady=true;
                    }
                    //{"errno": "0", "errmsg": "", "data": {"username": "haixng", "count": 0}}
                    else{
                        message.showError("用户名已经存在");
                        return
                    }
                },
                error(xhr,msg){
                    message.showError("服务器超时,请重试");
                    return
                }
            });
        }
     let smss=$('input[name=sms_captcha]');
        smss.blur(fnSmsCode);
        function fnSmsCode() {
            isSmsCode=false;
            let smscodetext=smss.val();
            if(smscodetext===''){
                message.showError("短信验证码不能为空");
                return
            }
            if(!(/\w{4}$/).test(smscodetext)){
                message.showError("短信验证码长度不合格");
                return
            }
            else{
                isSmsCode=true;
                return
            }
        }



     let checkMobile=$('input[name=telephone]');
        checkMobile.blur(fnCheckMobile);
        function fnCheckMobile() {
            isMobileReady=false;
            mobile=checkMobile.val();
            if(mobile===''){
                message.showError("手机号不能为空");
                return
            }
            if(!(/^1[3-9]\d{9}$/).test(mobile)){
                message.showError("手机号格式不正确");
                return
            }
            $.ajax({
                url:'/image/mobile/'+mobile+'/',
                type:'GET',
                dataType:'json',
                success:function (data) {
                    if(data.data.count ===0){
                        message.showInfo(mobile+"手机号可用");
                        isMobileReady=true;
                    }
                    else{
                        message.showError(mobile+"手机号已经注册");
                        return
                    }
                },
                error(xhr,msg){
                    message.showError("服务器超时,请重试");
                    return
                }
            })
        }


        let check_password=$('#password'),
            check_repassword=$('#password_repeat');
        check_repassword.blur(fnCheckPassword);
        function fnCheckPassword() {
            isPasswordReady=false;
            password=check_password.val();
            re_password=check_repassword.val();
            if(password===''){
                message.showError("密码不能为空");
                return
            }
            if(!(password===re_password)){
                message.showError('密码不一致,请重新确认');
                return
            }
            if(!(/\w{6,20}$/).test(password)){
                message.showError("密码长度太短,请重新设置");
                return
            }
            else{
                isPasswordReady=true;
                return
            }
        }



    let $smscode=$('.sms-captcha');
        $smscode.click(function () {
        let captcha=$('input[name=captcha_graph]').val();
        if(!isMobileReady){
            fnCheckMobile();
            return
        }
        if(captcha===''){
            message.showError("图形验证码不能为空");
            return
        }
        $.ajax({
            url:'/image/'+'sms_code/',
            type:'POST',
            dataType:'json',
            data:{
                mobile:checkMobile.val(),
                captcha:captcha,
            },
            success:function (data) {
                if(data.errno==='0'){
                    message.showSuccess(data.errmsg);
                    $smscode.attr('disabled',true);
                    //设置计时器
                    var num = 60;
                    let t = setInterval(function () {
                        $smscode.html('倒计时'+ num +'秒');
                        if(num===1){
                            clearInterval(t);
                            $smscode.removeAttr('disabled');
                            $smscode.html('获取短信验证码')
                        }num --
                    },1000)
                }
                else{
                    message.showError(data.errmsg);
                    return
                }
            },
            error(xhr,msg){
                message.showError("服务器超时,请重试");
                return
            }
        })
    });
    let btn=$('.register-btn');
    btn.click(function (e) {
        e.preventDefault();
        if(!isUsernameReady){
            fnCheckUsename();
            return
        } else if(!isPasswordReady){
            fnCheckPassword();
            return
        } else if(!isMobileReady){
            fnCheckMobile();
            return
        }else if(!isSmsCode){
            fnSmsCode();
            return
        }
        $.ajax({
            url:'/user/register/',
            type:"POST",
            dataType:'json',
            data:{
                username:user.val(),
                password:check_password.val(),
                password_repeat:check_repassword.val(),
                mobile:checkMobile.val(),
                smscode:smss.val(),
            },
            success:function (res) {
                if(res.errno==='0'){
                    message.showSuccess(res.errmsg);
                    setTimeout(function () {
                        window.location.href='/user/login/'
                    },3000)
                }
                else {
                    message.showError(res.errmsg);
                    return
                }
            },
            error(xhr,msg){
                message.showError("服务器超时，请重试");
                return
            }
        })

    })



});